<%@include file="/WEB-INF/common/taglibs.jsp"%>
<div class="tab-pane fade" id="stockTab">
<!--quantityUnitGroup --><div class="form-row row-fluid"><div class="span12"><div class="row-fluid"><label class="form-label span3">Quantity Unit :</label><div class="span4 controls"><select name="quantityUnitId">
<c:if test="${not empty quantityUnitGroups}"><c:forEach var="singleGroup" items="${quantityUnitGroups}">
<optgroup label="${singleGroup.name}">
<c:forEach var="single" items="${singleGroup.quantityUnits}">
<option value="${single.id}"${(single.id == entity.productStock.quantityUnit.id)?'selected':'' }>${single.name}   -  ${singleGroup.name}</option>
</c:forEach>
</optgroup>
</c:forEach></c:if>
</select></div></div></div></div>

<!--quantity --><div class="form-row row-fluid"><div class="span12"><div class="row-fluid"><label class="form-label span3" for="quantity">Quantity :</label><input class="span4" disabled="disabled" id="productStockQuantity"   type="text" value="${entity.productStock.quantity / entity.productStock.quantityUnit.basicRate   }" /></div></div></div>
<!--subtractStock --><div class="form-row row-fluid"><div class="span12"><div class="row-fluid"><label class="form-label span3" for="subtractStock">Subtract Stock :</label><div class="left marginT5 marginR10">
<div class="normal-toggle-button">
<input    <c:if test= "${entity.productStock.subtractStock}"> checked=checked</c:if>   type="checkbox" class="nostyle" fname="subtractStock">
<input class="nostyle" type="radio" name="subtractStock" id="subtractStock1" value="true"  <c:if test= "${entity.productStock.subtractStock}"> checked=checked</c:if>   style="opacity: 0;">
<input class="nostyle" type="radio" name="subtractStock" id="subtractStock2" value="false" <c:if test= "${!entity.productStock.subtractStock}"> checked=checked</c:if>  style="opacity: 0;">
</div>
</div>
</div>
</div>
</div>
<!--outOfStockStatus --><div class="form-row row-fluid"><div class="span12"><div class="row-fluid"><label class="form-label span3" for="outOfStockStatus">Out Of Stock Status :</label><div class="left marginT5 marginR10">
<div class="normal-toggle-button">
<input    <c:if test= "${entity.productStock.outOfStockStatus}"> checked=checked</c:if>   type="checkbox" class="nostyle" fname="outOfStockStatus">
<input class="nostyle" type="radio" name="outOfStockStatus" id="outOfStockStatus1" value="true"  <c:if test= "${entity.productStock.outOfStockStatus}"> checked=checked</c:if>   style="opacity: 0;">
<input class="nostyle" type="radio" name="outOfStockStatus" id="outOfStockStatus2" value="false" <c:if test= "${!entity.productStock.outOfStockStatus}"> checked=checked</c:if>  style="opacity: 0;">
</div>
</div>
</div>
</div>
</div>
<script>


</script>
<div class="form-row row-fluid">
								<div class="span12">
									<div class="row-fluid">
										<label class="form-label span3" for="price">Price :</label><input
											class="span4" id="price"   type="text" name="viewPrice"
											value="${entity.viewPrice}" />
									</div>
								</div>
							</div>
							<!--marketPrice -->
<div class="form-row row-fluid">
								<div class="span12">
									<div class="row-fluid">
										<label class="form-label span3" for="marketPrice">MarketPrice
											:</label><input class="span4" id="marketPrice"  name="viewMarketPrice"
											type="text" value="${entity.viewMarketPrice }" />
									</div>
								</div>
</div>
							
							<!--minimumQuantity -->
							<div class="form-row row-fluid">
								<div class="span12">
									<div class="row-fluid">
										<label class="form-label span3" for="minimumQuantity">Minimum
											Quantity :</label><input class="span4" id="minimumQuantity"
										  type="text"  name="viewMinimumQuantity"
											value="${entity.viewMinimumQuantity}" />
									</div>
								</div>
							</div>


<c:if test="${entity.productStock ne null }">

<div class="form-row row-fluid">
<div id="stock-table" class="span12">
	<div class="box gradient">
	 
		<div class="title">
				<h4>
				<span>Stock Balance </span> <span class="box-form right"> </span>
				
				<span class="box-form right stockBalance_bar"><a href="#" class="btn btn-mini addStockBalance">Increase Stock</a>
				
				<a href="#" class="btn btn-mini deductBalance">Deduct Stock</a>
				</span>
 				</h4>
 				<a href="#" class="minimize"> </a>
			</div>
		 <div class="content scrollable clearfix">
		<table class="table table-bordered table-vertical-centre stockBalanceTable">
			<thead>
				<tr>
<th>Time </th>
<th>Amount </th>
<th>Type </th>
<th>Balance </th>
<th>Description </th>

				</tr>
			</thead>
		</table>
		</div>
</div>
 </div>
 </div>


<script>

$(document).ready(function(){
	$('.addStockBalance').click(function(){
		$("#stockBalanceDiv").find("div.modal-body").find("input,select,textarea,checkbox").not("[name=action],[type=hidden]").not("[type=radio]").each(function(step,value){
			 									$(value).val("");
		});
		 $("#stockBalanceDiv input[name='action']").val("${ctx}/admin/stock-balance/${entity.id}/increase/product/");
	 	 $("#stockBalanceDiv").modal('show');
	  });
	
	$('.deductBalance').click(function(){
		$("#stockBalanceDiv").find("div.modal-body").find("input,select,textarea,checkbox").not("[name=action],[type=hidden]").not("[type=radio]").each(function(step,value){
			 									$(value).val("");
		});
		 $("#stockBalanceDiv input[name='action']").val("${ctx}/admin/stock-balance/${entity.id}/deduct/product/");
	 	 $("#stockBalanceDiv").modal('show');
	  });
	
	
	
	$('.modal-submit-stock').click(function(){
 		var div  = $(this).parents("div.modal:first");
 		var dd  = div.find("select, textarea, input").serializeArray();
 		$(dd).each(function(){
 			if (this.name=='amount'){
				this.value = this.value * ${entity.productStock.quantityUnit.basicRate}
 			}

 		});
  				$.ajax({
					url : $(div).find("input[name='action']").val(),
					type : "POST",
					data : dd,
					dataType : "json",
					success : function(data){		
						if(data && data.state && (data.state.indexOf("ERROR") != -1)){
							johnny.error(data.message);
							return;
						}else{
 								div.modal('hide');
 								
 								var x  = data.entity /  ${entity.productStock.quantityUnit.basicRate};
 								$("#productStockQuantity").val(x);
 								
							var x =	div.attr('selector');
								$(x).dataTable().fnDraw();								
 								johnny.info("operate successfully");
							}
					}
				});
 	});

	
	
	
	
		$("a[href='#stockTab']").click(function (e) {
			   if (!$(this).hasClass("inited")){
				   $(this).addClass("inited");
				   
				   setTimeout(function(){
					   
				 		var recordColumn = ['time','amount','balance','description'];
						
						 $('.stockBalanceTable').dataTable({
							"bLengthChange" : true,
					 		"sAjaxSource" : '${ctx}/admin/stock-balance/${entity.id}/product/',
							"createQueryData" : function (aoData){
									return	johnny.initQueryData(aoData, recordColumn);
							},
							"createResult" : function (json, echo){
					 			var dataArray = new Array();
								for ( var i = 0; i < json.result.length; i++) {
									var subArray = new Array();
					subArray.push(json.result[i].time)
					subArray.push(json.result[i].amount /  ${entity.productStock.quantityUnit.basicRate})
					subArray.push(json.result[i].type)
					subArray.push(json.result[i].balance / ${entity.productStock.quantityUnit.basicRate})
					subArray.push(json.result[i].description)
					  				dataArray.push(subArray);
								}
								return {
									sEcho : echo,
									iTotalRecords : json.totalCount,
									iTotalDisplayRecords : json.totalCount,
									aaData : dataArray
								};
							}		
						});
					   
					   
				   },400);

			   }

		});
 
 
	
});


</script>

</c:if>
</div>